package org.ResearchProject.controller;
import org.ResearchProject.dto.Result;
import org.ResearchProject.pojo.Student;
import org.ResearchProject.pojo.Tutor;
import org.ResearchProject.pojo.User;
import org.ResearchProject.service.StudentService;
import org.ResearchProject.service.TutorService;
import org.ResearchProject.utils.JwtUtil;
import org.ResearchProject.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * 用户相关接口
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    StudentService studentService;
    @Autowired
    TutorService tutorService;
    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Result register(@RequestBody @Validated(User.register.class) User user){
        if (user.getAuth() == 1){
            // 插入学生
            studentService.addStudent(user);
            return Result.success();
        }
        if (user.getAuth() == 0){
            // 插入导师
            tutorService.addTutor(user);
            return Result.success();
        }
        return Result.error("用户类型错误");
    }

    /**
     * 用户登录
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody @Validated(User.login.class)  User user){
        if (user.getAuth() == 1){
            // 验证是否为学生
            Student student = studentService.selectByUsername(user.getUsername());
            if (student==null)
                return Result.error("用户不存在");
            if (!Objects.equals(user.getPassword(), student.getPassword()))
                return Result.error("密码错误");
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", user.getUsername());
            claims.put("auth", user.getAuth());
            claims.put("id", student.getId());
            String token = JwtUtil.genToken(claims);
            Map<String, Object> msg = new HashMap<>();
            msg.put("user", student);
            msg.put("token", token);
            return Result.success(msg);
        }
        if (user.getAuth() == 0){
            Tutor tutor = tutorService.selectByUsername(user.getUsername());
            if (tutor==null)
                return Result.error("用户不存在");
            if (!Objects.equals(user.getPassword(), tutor.getPassword()))
                return Result.error("密码错误");
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", user.getUsername());
            claims.put("auth", user.getAuth());
            claims.put("id", tutor.getId());
            String token = JwtUtil.genToken(claims);
            Map<String, Object> msg = new HashMap<>();
            msg.put("user", tutor);
            msg.put("token", token);
            return Result.success(msg);
        }
        return Result.error("用户类型错误");
    }
    /**
     * 修改用户信息
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody User user){
        if (user.getAuth() == 1){
            // 修改学生
            studentService.updateStudent(user);
            return Result.success();
        }
        if (user.getAuth() == 0){
            // 修改导师
            tutorService.updateTutor(user);
            return Result.success();
        }
        return Result.success();
    }
    /**
     * 修改密码
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public Result updatePassword(@RequestBody Map<String,String> map){
        Map<String, Object> msg = ThreadLocalUtil.get();
        if ("".equals(map.get("oldPassword"))||"".equals(map.get("newPassword"))||"".equals(map.get("newPassword2")))
            return Result.error("密码不能为空");
        if (!Objects.equals(map.get("newPassword"), map.get("newPassword2")))
            return Result.error("两次密码不一致");
        if ((int)msg.get("auth") == 0) {
            //导师
            Tutor tutor = tutorService.selectByUsername((String) msg.get("username"));
            if (!Objects.equals(map.get("oldPassword"), tutor.getPassword()))
                return Result.error("旧密码错误");
            tutorService.updateTutorPwd(map.get("newPassword"), tutor.getUsername());
        }
        return Result.success();
    }

}
